#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "delay.h"
#include "mh22xx.h"
#include "bsp_ili9488_lcd.h"
#include "fonts.h"
#include "3DTest.h"

#define PRINTF_LOG 	printf

USART_TypeDef* USART_TEST = USART1;

void UART_Configuration(uint32_t bound);
void RCC_ClkConfiguration(void);
void LCD_Test(void);	
void Delay ( __IO uint32_t nCount );

	
int main(void)
{	
	RCC_ClocksTypeDef clocks;
	
	RCC_ClkConfiguration();
	Delay_Init();
	UART_Configuration(115200);
	
	RCC_GetClocksFreq(&clocks);
	
	PRINTF_LOG("\n");
	PRINTF_LOG("SYSCLK: %3.1fMhz, HCLK: %3.1fMhz, PCLK1: %3.1fMhz, PCLK2: %3.1fMhz\n", \
	(float)clocks.SYSCLK_Frequency/1000000, (float)clocks.HCLK_Frequency/1000000, \
	(float)clocks.PCLK1_Frequency/1000000, (float)clocks.PCLK2_Frequency / 1000000);
	
	PRINTF_LOG("ILI9488 320x480 4Inch 3D Test.\n");
	
	ILI9488_Init();

	SysTick_Config(clocks.SYSCLK_Frequency/1000);
	while ( 1 )
	{
		loop();
	}
}

#if 0
/*用于测试各种液晶的函数*/
void LCD_Test(void)
{
	/*演示显示变量*/
	static uint8_t testCNT = 0;	
	char dispBuff[100];

	testCNT++;	

	LCD_SetFont(&Font8x16);
	LCD_SetColors(RED,BLACK);

	ILI9488_Clear(0,0,LCD_X_LENGTH,LCD_Y_LENGTH);	/* 清屏，显示全黑 */
	/********显示字符串示例*******/
	ILI9488_DispStringLine_EN(LINE(0),"4inch LCD para:");
	ILI9488_DispStringLine_EN(LINE(1),"Image resolution:320x480 px");
	ILI9488_DispStringLine_EN(LINE(2),"ILI9488 LCD driver");
	ILI9488_DispStringLine_EN(LINE(3),"MEGAHUNT");
	
	/********显示变量示例*******/
	LCD_SetFont(&Font16x24);
	LCD_SetTextColor(GREEN);

	/*使用c标准库把变量转化成字符串*/
	sprintf(dispBuff,"Count : %d ",testCNT);
	LCD_ClearLine(LINE(4));	/* 清除单行文字 */

	/*然后显示该字符串即可，其它变量也是这样处理*/
	ILI9488_DispStringLine_EN(LINE(4),dispBuff);

	/*******显示图形示例******/
	LCD_SetFont(&Font24x32);
	/* 画直线 */

	LCD_ClearLine(LINE(4));/* 清除单行文字 */
	LCD_SetTextColor(BLUE);

	ILI9488_DispStringLine_EN(LINE(4),"Draw line:");

	LCD_SetTextColor(RED);
	ILI9488_DrawLine(50,170,210,400);  
	ILI9488_DrawLine(50,200,210,350);

	LCD_SetTextColor(GREEN);
	ILI9488_DrawLine(100,170,200,330);  
	ILI9488_DrawLine(200,200,220,340);

	LCD_SetTextColor(BLUE);
	ILI9488_DrawLine(110,170,110,330);  
	ILI9488_DrawLine(130,200,220,440);

	Delay(0xFFFFFF);

	ILI9488_Clear(0,16*8,LCD_X_LENGTH,LCD_Y_LENGTH-16*8);	/* 清屏，显示全黑 */

	/*画矩形*/

	LCD_ClearLine(LINE(4));	/* 清除单行文字 */
	LCD_SetTextColor(BLUE);

	ILI9488_DispStringLine_EN(LINE(4),"Draw Rect:");

	LCD_SetTextColor(RED);
	ILI9488_DrawRectangle(50,200,100,30,1);

	LCD_SetTextColor(GREEN);
	ILI9488_DrawRectangle(160,200,20,40,0);

	LCD_SetTextColor(BLUE);
	ILI9488_DrawRectangle(170,200,50,20,1);


	Delay(0xFFFFFF);

	ILI9488_Clear(0,16*8,LCD_X_LENGTH,LCD_Y_LENGTH-16*8);	/* 清屏，显示全黑 */

	/* 画圆 */
	LCD_ClearLine(LINE(4));	/* 清除单行文字 */
	LCD_SetTextColor(BLUE);

	ILI9488_DispStringLine_EN(LINE(4),"Draw Cir:");

	LCD_SetTextColor(RED);
	ILI9488_DrawCircle(100,200,40,0);

	LCD_SetTextColor(GREEN);
	ILI9488_DrawCircle(100,200,20,1);

	LCD_SetTextColor(BLUE);
	ILI9488_DrawCircle(140,200,40,0);

	Delay(0xFFFFFF);

	ILI9488_Clear(0,16*8,LCD_X_LENGTH,LCD_Y_LENGTH-16*8);	/* 清屏，显示全黑 */
}
#endif

/**
  * @brief  简单延时函数
  * @param  nCount ：延时计数值
  * @retval 无
  */	
void Delay ( __IO uint32_t nCount )
{
  for ( ; nCount != 0; nCount -- );
	
}

void RCC_ClkConfiguration(void)
{
	RCC_DeInit();

	RCC_HSEConfig(RCC_HSE_ON);
	while(RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET);
	
	RCC_PLLCmd(DISABLE);
	
	FLASH_SetLatency(FLASH_Latency_2);
	
	RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_27);
	
	RCC_PLLCmd(ENABLE);
	while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
	
	RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
	
	RCC_HCLKConfig(RCC_SYSCLK_Div1);
	RCC_PCLK1Config(RCC_HCLK_Div2);
	RCC_PCLK2Config(RCC_HCLK_Div1);
	
	RCC_LSICmd(ENABLE);
	while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET);
	RCC_HSICmd(ENABLE);
	while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET);
}

void UART_Configuration(uint32_t bound)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; 
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;	
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	USART_InitStructure.USART_BaudRate = bound;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

	USART_Init(USART_TEST, &USART_InitStructure);
	USART_Cmd(USART_TEST, ENABLE);
}


int SER_PutChar (int ch)
{
	while(!USART_GetFlagStatus(USART_TEST,USART_FLAG_TC));
	USART_SendData(USART_TEST, (uint8_t) ch);

	return ch;
}

int fputc(int c, FILE *f)
{
	/* Place your implementation of fputc here */
	/* e.g. write a character to the USART */
	if (c == '\n')
	{
		SER_PutChar('\r');
	}
	return (SER_PutChar(c));
}

